package com.shundr.shipper.route;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.view.CListView;
import com.shundr.shipper.common.view.CListView.OnLoadMoreListener;
import com.shundr.shipper.common.view.CListView.OnRefreshListener;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.route.adapter.RouteAdapter;
import com.shundr.shipper.route.http.RouteRequest;
import com.shundr.shipper.route.model.TruckerRouteInfo;

/**
 * 常跑路线
 * RouteListActivity
 * 
 * @author larry 2015-4-22 上午11:28:08 
 * @version 1.0.0
 *
 */
public class RouteListActivity extends BaseActivity {
	
	private RouteAdapter mAdapter;
	
	private List<TruckerRouteInfo> mList = new ArrayList<TruckerRouteInfo>();
	
	private CListView mListView;
	private RelativeLayout mLayoutNoContent;
	private TextView tvNoContent;
	
	private Button btnAdd;
	
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case RouteRequest.GET_ROUTE_LIST_SUCCESSED:
				@SuppressWarnings("unchecked")
				List<TruckerRouteInfo> infos = (List<TruckerRouteInfo>)msg.obj;
				if (infos != null) {
					if (infos.size() > 0) {
						mList.clear();
						mListView.setVisibility(View.VISIBLE);
						mLayoutNoContent.setVisibility(View.GONE);
						mList.addAll(infos);
						mAdapter.notifyDataSetChanged();
					} else {
						mListView.setVisibility(View.GONE);
						mLayoutNoContent.setVisibility(View.VISIBLE);
					}
				}
//				ProgressDialogHelper.dismiss();
				mListView.onRefreshComplete();
				mListView.onLoadMoreComplete();
				break;
			case LHttpRequest.REQUEST_FAILED:
				mListView.onLoadMoreComplete();
				mListView.onRefreshComplete();
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_route_list);
		
		mListView = (CListView) findViewById(R.id.lv_list);
		mLayoutNoContent = (RelativeLayout) findViewById(R.id.layout_no_content);
		tvNoContent = (TextView) findViewById(R.id.tv_no_content);
		btnAdd = (Button)findViewById(R.id.btn_add);
		btnAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//添加页面
				startActivityForResult(new Intent(mContext, RouteAddActivity.class), 500);
			}
		});
		mLayoutNoContent.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//添加页面
				startActivityForResult(new Intent(mContext, RouteAddActivity.class), 500);
			}
		});
//		btnRight.setVisibility(View.VISIBLE);
//		btnRight.setBackgroundResource(R.drawable.iv_setting);
//		btnRight.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				//设置页面
//				startActivityForResult(new Intent(mContext, RouteSettingActivity.class), 500);
//			}
//		});
//		tvTitle.setText("常跑路线");
		tvNoContent.setText("您还未添加常跑路线,点我添加");
		mAdapter = new RouteAdapter(mList, mContext);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
			}
			
		});
		
		mListView.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				new RouteRequest(mContext, mHandler).getRouteList();
			}
		});
		mListView.setOnLoadListener(new OnLoadMoreListener() {
			@Override
			public void onLoadMore() {
				new RouteRequest(mContext, mHandler).getRouteList();
			}
		});
		new RouteRequest(mContext, mHandler).getRouteList();
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		switch (requestCode) {
		case 500:
			new RouteRequest(mContext, mHandler).getRouteList();
			break;

		default:
			break;
		}
	}
	
	
	
}
